﻿namespace Growatt.ShinebusServiceUploadPack.Model
{
    using System;

    public class FeishuInfoRequest
    {
        public FeishuInfoRequest(string previousFirmwareName, string currentFirmwarePackageName, string feishuTableUrl, List<string> uploadToOfficialServer, string chineseUpdateLog, string currentFirmwarePackagePath)
        {
            OldPackagePath = previousFirmwareName;
            CurrentFirmwarePackageName = currentFirmwarePackageName;
            FeishuTableUrl = feishuTableUrl;
            UploadToOfficialServer = uploadToOfficialServer;
            ChineseUpdateLog = chineseUpdateLog;
            CurrentFirmwarePackagePath = currentFirmwarePackagePath;
        }

        public FeishuInfoRequest()
        {
            // 默认构造函数可以在这里初始化一些默认值，如果需要的话。
        }
        public static (string baseId, string tableId, string viewId) ParseFeishuUrl(string url)
        {
            // 使用Uri类解析URL
            Uri uri = new Uri(url);

            // 获取路径段并分割
            string[] pathSegments = uri.Segments;
            string baseId = pathSegments.Length > 3 ? pathSegments[3].TrimEnd('/') : null;

            // 解析查询字符串
            var queryParameters = uri.Query.TrimStart('?').Split('&')
                .Select(segment => segment.Split('='))
                .ToDictionary(keyValue => keyValue[0], keyValue => keyValue[1]);

            string tableId = queryParameters.ContainsKey("table") ? queryParameters["table"] : null;
            string viewId = queryParameters.ContainsKey("view") ? queryParameters["view"] : null;

            return (baseId, tableId, viewId);
        }
        public string OldPackagePath { get; set; }
        public string CurrentFirmwarePackageName { get; set; }
        public string CurrentFirmwarePackagePath { get; set; }
        public string FeishuTableUrl { get; set; }
        public List<string> UploadToOfficialServer { get; set; }
        public string ChineseUpdateLog { get; set; }
    }

}
